import api from '@/services'
const Base64 = require("js-base64").Base64
const state = {
    user: {
        id: null,
        organizationId: null,
        loginName: "",
        userName: "",
        phone: "",
        role: "",
        createTime: ""
    },
}

const mutations = {
    setUser(state, user) {
        state.user = user
    },

}

const actions = {
    async initUser({ commit, state }) {
        let loginName = window.localStorage.getItem("userId");
        if (loginName) {
            let password = Base64.decode(window.localStorage.getItem("password"));// base64解密
            let user = await api.user.login(loginName, password).then(res => res.data.user);
            commit('setUser', user);
        }
    }
}

export default {
    state,
    actions,
    mutations,
}